// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Q [author] rhyme_lph
// P [email]  rhymelph@gmail.com
// f [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/coa_entity.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/provider/all_reports/journal/account_amount_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:flutter/material.dart';

export 'package:finance_app/src/entities/coa_entity.dart';

@RouterMeta(paramName: "accountAmountPage")
class AccountAmountPage extends StatefulWidget {
  final CoaCoaAccountHeaderCoaAccountDetail account;
  final SettingDataTax gst;
  final bool isCheckInclusive;
  final num amount;

  const AccountAmountPage(
      {Key key, this.account, this.amount, this.isCheckInclusive, this.gst})
      : super(key: key);

  @override
  _AccountAmountPageState createState() => _AccountAmountPageState();
}

class _AccountAmountPageState extends State<AccountAmountPage>
    with PagePresenter<AccountAmountPage, AccountAmountProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.account__amount, actions: [
        if (provider.isShowDelete == true)
          NormalDeleteTextButton(
            onTap: provider.onDelete,
          ),
      ]);

  @override
  void initState() {
    provider.account = widget.account;
    if (widget.isCheckInclusive != null) {
      provider.isCheckInclusive = widget.isCheckInclusive;
    }
    if (widget.gst != null) {
      provider.gstRate = widget.gst;
    }
    if (widget.amount != null) {
      provider.amountController.text =
          CashUtils.formatCashAmount2(widget.amount);
      provider.isShowDelete = true;
    }
    super.initState();
  }

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            listBuilder<String>(
              selector: (_, __) => __.account?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.account,
                value: value,
                onTap: provider.onChangeAccount,
              ),
            ),
            FromInputField(
              tip: S.current.amount,
              controller: provider.amountController,
              focusNode: provider.amountFocusNode,
              inputFormatterList: [
                doubleInputFormatter,
              ],
              keyboardType: TextInputType.numberWithOptions(decimal: true),
            ),
            // Visibility(
            //   visible: Config.currentCompany.enableGst == 1,
            //   child: listBuilder<String>(
            //     selector: (_, __) => __.gstRate?.displayName,
            //     builder: (BuildContext context, String value, Widget child) =>
            //         Column(
            //       children: [
            //         ValueSelectForm(
            //           onTap: provider.onSelectGstRate,
            //           title: S.current.gst_rate,
            //           value: value,
            //         ),
            //         listBuilder<bool>(
            //           selector: (_, __) => __.enable,
            //           builder: (BuildContext context, bool value,
            //                   Widget child) =>
            //               value == true
            //                   ? listBuilder<Tuple3<bool, String, String>>(
            //                       selector: (_, __) => Tuple3(
            //                           __.isCheckInclusive,
            //                           __.getGSTInfo(),
            //                           __.getTotal()),
            //                       builder: (BuildContext context,
            //                               Tuple3<bool, String, String> value,
            //                               Widget child) =>
            //                           NormalFormWrapper(
            //                         child: GstInclusiveForm(
            //                           isCheckInclusive: value.item1,
            //                           gstInfo: value.item2,
            //                           total: value.item3,
            //                           onChangeInclusive:
            //                               provider.onChangeInclusive,
            //                         ),
            //                       ),
            //                     )
            //                   : Container(),
            //         ),
            //       ],
            //     ),
            //   ),
            // ),
            AppSpaces.h30,
            listBuilder<bool>(
              selector: (_, __) => __.enable,
              builder: (BuildContext context, bool value, Widget child) =>
                  NormalButton(
                title: S.current.done,
                onTap: value == true ? provider.onDone : null,
              ),
            ),
          ],
        ),
      );
}
